// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.*;
import com.fowo.api.model.nav.sell.order.NavSellOrderVo;
import com.fowo.api.model.nav.sell.order.detail.NavSellOrderDetailItemVo;
import com.fowo.api.model.parilist.ParilistVo;
import com.fowo.api.sys.annotation.DataActivity;
import com.fowo.api.sys.entity.enums.SysDataActivityAction;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 配对列表 服务实现类
 */
@Service
public class ParilistServiceImpl extends ParilistBaseServiceImpl {

    @Resource
    private ProductServiceImpl productService;
    @Resource
    private ShopInfoServiceImpl shopInfoService;
    @Resource
    private WalmartListingServiceImpl walmartListingService;
    @Resource
    private TiktokProductServiceImpl tiktokProductService;


    /**
     * 获取配对列表
     * @param shopInfo
     * @param msku
     * @return
     */
    public Parilist getParilist(Long shopInfo,String msku) {
        Parilist parilist = this.getOne(Wrappers.lambdaQuery(Parilist.class)
                .eq(Parilist::getMsku, msku)
                .eq(Parilist::getShopId, shopInfo)
                .eq(Parilist::getType, "1"),false);
        return parilist;
    }

    /**
     * 创建配对列表
     * @param model 配对列表
     * @return 新数据的主键
     */
    @DataActivity(action = SysDataActivityAction.Add)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long create(ParilistVo model) throws Exception {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
//        Parilist entity;
        Parilist entity = this.getParilist( model.getShopId(),model.getMsku());
        if(null == entity){
            entity = new Parilist();
            BeanUtils.copyProperties(model, entity);
        }else {
            Assert.isFalse(StringUtils.equals(entity.getType(),"1"),model.getMsku()+"已配对");
            entity.setSku(model.getSku());
        }
        Product product;
        if(null != model.getProductId()){
            product = productService.getById(model.getProductId());
            entity.setSku(product.getSku());
        }else {
            product = productService.getOne(Wrappers.lambdaQuery(Product.class).eq(Product::getSku, model.getSku()));
            entity.setProductId(product.getId());
        }
        entity.setImg(product.getPicUrl());
        entity.setProductName(product.getName());
        final ShopInfo shopInfo = shopInfoService.getById(model.getShopId());
        entity.setPlatforms(shopInfo.getPlatform());
        entity.setCreateTime(new Date());
        entity.setLastUpdateTime(new Date());
        if (currentUser != null) {
            entity.setCreateUser(currentUser.getUserId());
            entity.setLastUpdateUser(currentUser.getUserId());
        }

        entity.setType("1");
        if (this.saveOrUpdate(entity)) {
            updateOnlineProduct(entity);
            return entity.getId();
        }
        throw new Exception("配对列表保存失败");
    }



    @DataActivity(action = SysDataActivityAction.Modified)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ParilistVo model) throws Exception {

        Parilist parilist = this.getOne(Wrappers.lambdaQuery(Parilist.class)
                .eq(Parilist::getMsku, model.getSku())
                .eq(Parilist::getShopId, model.getShopId())
                .eq(Parilist::getSku,model.getSku())
                .eq(Parilist::getType,"1"));
        Assert.isNull(parilist,model.getMsku()+"已配对");
        final Product product = productService.getVoById(model.getProductId());
        model.setProductId(product.getId());
        model.setImg(product.getPicUrl());
        model.setSku(product.getSku());
        model.setProductName(product.getName());
        model.setType("1");
        final ShopInfo shopInfo = shopInfoService.getById(model.getShopId());
        model.setPlatforms(shopInfo.getPlatform());
        boolean update = super.updateById(model);
        updateOnlineProduct(model);
        return update;
    }
    public void updateOnlineProduct(Parilist entity){
        switch ( entity.getPlatforms()){
            case "Walmart":
                updateWalmartListing(entity);
                break;
            case "TikTok":
                updateTiktokListing(entity);
                break;

        }

    }

    public void updateWalmartListing(Parilist entity){
        WalmartListing walmartListing = walmartListingService.getOne(Wrappers.lambdaQuery(WalmartListing.class)
                .eq(WalmartListing::getSid,entity.getShopId())
                .eq(WalmartListing::getMsku,entity.getMsku()));
        if(null != walmartListing){
            walmartListing.setProductId(entity.getProductId());
            walmartListing.setLocalSku(entity.getSku());
            walmartListing.setLocalName(entity.getProductName());
            walmartListing.setLastUpdateTime(new Date());
            walmartListing.setLastUpdateUser(entity.getLastUpdateUser());
            walmartListingService.updateById(walmartListing);
        }
    }

    public void updateTiktokListing(Parilist entity){
        List<TiktokProduct> tiktokProducts = tiktokProductService.list(Wrappers.lambdaQuery(TiktokProduct.class)
                .eq(TiktokProduct::getSid,entity.getShopId())
                .eq(TiktokProduct::getSellerSku,entity.getMsku()));
        for(TiktokProduct tiktokProduct : tiktokProducts){
            tiktokProduct.setLocalProduct(entity.getProductId());
            tiktokProduct.setSku(entity.getSku());
            tiktokProduct.setLastUpdateTime(new Date());
            tiktokProduct.setLastUpdateUser(entity.getLastUpdateUser());
            tiktokProductService.updateById(tiktokProduct);
        }
    }
}
